import openpyxl


class HandleExcel:
    def __init__(self, filename, sheetname):
        '''
        :param filename: excel文件路径
        :param sheetname: sheet表单名
        '''
        self.filename = filename
        self.sheetname = sheetname

    def get_data(self):
        workbook = openpyxl.load_workbook(filename=self.filename)
        sheet = workbook[self.sheetname]
        res = list(sheet.rows)
        title = [i.value for i in res[0]]
        cases = []
        for item in res[1:]:
            data = [i.value for i in item]
            case = dict(zip(title, data))
            cases.append(case)
        return cases

    def write_data(self, row, colum, value):
        '''
        :param row: 要写入的行
        :param colum: 要写入的列
        :param value: 要写入的值
        :return:
        '''
        workbook = openpyxl.load_workbook(self.filename)
        sheet = workbook[self.sheetname]
        sheet.cell(row, colum, value)
        workbook.save(self.filename)



